<?php
/**
 * Created by PhpStorm.
 * User: Dell
 * Date: 2019-3-4
 * Time: 11:16
 */

namespace app\admin\controller;


use app\common\model\ClassifyModel;
use app\common\model\GoodsModel;

//花椒 1
class Pepper extends Base
{
    // 商品列表
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where['good_delete'] = 0;
            $where['good_type'] = 1;
            if (!empty($param['searchText'])) {
                $where['good_name'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $goods = new GoodsModel();
            $selectResult = $goods->getGoodsByWhere($where, $offset, $limit);

            $classify = new ClassifyModel();

            $rows = $classify->getClassifySon(['clas_parentId'=>1]);
            $arr = [];
            foreach ($rows as $k=>$v){
                $arr[$v['clas_id']] = $v['clas_name'];
            }

            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['good_classify'] = $arr[$vo['good_classify']];
                $selectResult[$key]['good_img'] = '<img src="' . $vo['good_img'] . '" width="40px" height="40px">';
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['good_id']));
            }

            $return['total'] = $goods->getAllGoods($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    // 添加商品
    public function goodsAdd()
    {
        if(request()->isPost()){
            $param = input('post.');

            unset($param['file']);
            $param['good_createTime'] = date('Y-m-d H:i:s');
            $param['good_editTime'] = date('Y-m-d H:i:s');
            $param['good_type'] = 1;

            $goods = new GoodsModel();


            $flag = $goods->addGoods($param,'pepper/index');

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $clasify = new ClassifyModel();
        $where['clas_delete'] = 0;
        $where['clas_parentId'] = 1;
        $result = $clasify->getClassifyAll($where);
        $this->assign([
            'classify'=>$result
        ]);
        return $this->fetch();
    }

    public function goodsEdit()
    {
        $goods = new GoodsModel();
        if(request()->isPost()){

            $param = input('post.');
            unset($param['file']);
            $param['good_editTime'] = date('Y-m-d H:i:s');
            $param['good_type'] = 1;
            $flag = $goods->editGoods($param,'pepper/index');
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.good_id');
        $clasify = new ClassifyModel();
        $where['clas_delete'] = 0;
        $where['clas_parentId'] = 1;
        $result = $clasify->getClassifySon($where);
        $this->assign([
            'goods' => $goods->getOneGoods($id),
            'classify'=>$result
        ]);
        return $this->fetch();
    }

    public function goodsDel()
    {
        $id = input('param.good_id');

        $goods = new GoodsModel();
        $flag = $goods->delGoods($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    // 上传缩略图
    public function uploadImg()
    {
        if(request()->isAjax()){

            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if($info){
                $src =  '/upload' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'pepper/goodsedit',
                'href' => url('pepper/goodsedit', ['good_id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'pepper/goodsdel',
                'href' => "javascript:goodsDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}